﻿namespace Harness
{
    using System;
    using System.Collections.Generic;
    using System.Linq;

    internal static class Verification
    {
        #region Constants

        private const int VerificationTypeArgIndex = 0;

        private static readonly FlyweightFactory<IVerificator> Flyweight = new FlyweightFactory<IVerificator>("Harness.Verificators", "Verificator");

        #endregion

        #region Public Methods

        public static void SyntaxCheck(IHarness harness, IEnumerable<object> args)
        {
            if (null == args.ElementAtOrDefault(VerificationTypeArgIndex))
            {
                throw new ArgumentNullException((VerificationTypeArgIndex + 1).ToString() + ":VerificationType");
            }
        }

        public static bool Verify(IHarness harness, IEnumerable<object> args)
        {
            string verificationType = args.ElementAtOrDefault(VerificationTypeArgIndex) as string;
            bool expected = !verificationType.StartsWith("!");

            if (false == expected)
            {
                verificationType = verificationType.Substring(1);
            }

            IVerificator verificator = Flyweight.Get(verificationType);
            List<object> vargs = args.ToList().GetRange(1, args.Count() - 1);
            verificator.SyntaxCheck(harness, vargs);
            bool actual = verificator.Verify(harness, vargs);

            return expected == actual;
        }

        public static bool IsTrue(IHarness harness, IEnumerable<object> args)
        {
            return true == Verify(harness, args);
        }

        public static bool IsFalse(IHarness harness, IEnumerable<object> args)
        {
            return false == Verify(harness, args);
        }

        #endregion
    }
}
